package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.ArticleDtoIn;
import com.zyw.zhuangdian.pojo.out.ArticleDtoOut;
import com.zyw.zhuangdian.service.ArticleService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/article")
@RequiredArgsConstructor
@Slf4j
public class ArticleController {
    private final ArticleService articleService;

    /**
     * 新增文章
     * @param json
     * @return
     */
    @RequestMapping(value = "addArticle",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addArticle(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"title","文章标题不能为空");
        Assert.hasKeyAndValue(json,"publisher","发布人不能为空");
        Assert.hasKeyAndValue(json,"type","文章类型不能为空");
        Assert.hasKeyAndValue(json,"content","文章内容不能为空");

        ArticleDtoIn in = BeanConvertUtil.covertBean(json, ArticleDtoIn.class);
        Resp<Void> resp = articleService.addArticle(in);
        return RespUtil.OK(resp);
    }

    /**
     * 修改文章
     * @param json
     * @return
     */
    @RequestMapping(value = "updateArticle",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> updateArticle(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"aid","主键ID不能为空");

        ArticleDtoIn in = BeanConvertUtil.covertBean(json, ArticleDtoIn.class);
        Resp<Void> resp = articleService.updateArticle(in);
        return RespUtil.OK(resp);
    }

    /**
     * 删除文章
     * @param json aid
     * @return
     */
    @RequestMapping(value = "deleteArticle",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteArticle(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"aid","主键ID不能为空");

        ArticleDtoIn in = BeanConvertUtil.covertBean(json, ArticleDtoIn.class);
        Resp<Void> resp = articleService.deleteArticle(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出文章
     * @param json
     * @return
     */
    @RequestMapping(value = "listArticle",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listArticle(@RequestBody JSONObject json){
        ArticleDtoIn in = BeanConvertUtil.covertBean(json, ArticleDtoIn.class);
        Resp<List<ArticleDtoOut>> resp = articleService.listArticles(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出用户编写的文章
     * @param json
     * @return
     */
    @RequestMapping(value = "listUserArticle",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listUserArticle(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"currentUid","发布者ID不能为空");
        Assert.hasKeyAndValue(json,"type","文章类型不能为空");

        ArticleDtoIn in = BeanConvertUtil.covertBean(json, ArticleDtoIn.class);
        in.setPublisher(in.getCurrentUid());
        Resp<List<ArticleDtoOut>> resp = articleService.listArticles(in);
        return RespUtil.OK(resp);
    }
}
